program solve_before;

class X;
    rand bit       a;
    rand bit [2:0] b;

    constraint a_b { (a == 1) -> b == 0; }
endclass

class Y;
    rand bit       a;
    rand bit [2:0] b;

    constraint sab { solve a before b;} // 指定约束求解的顺序
    constraint a_b { (a == 1) -> b == 0;}
endclass

class Array;
    rand bit [3:0] size;
    rand bit [3:0] data[];

    constraint c_order {
        solve size before data; // 指定约束求解的顺序
    }

    constraint c_size {
        data.size() == size;
        size > 0;
    }

    constraint c_elements {
        foreach (data[i]) {
            data[i] < size;
        }
    }
endclass

initial begin
    begin
        X x = new();
        $display("Without 'solve before' constraint");
        repeat(12) begin
            x.randomize();
            $display("\tValue of a = %0d, b = %0d",x.a,x.b);
        end
    end

    begin
        Y y = new();
        $display("With 'solve before' constraint");
        repeat(12) begin
            y.randomize();
            $display("\tValue of a = %0d, b = %0d",y.a,y.b);
        end
    end

    begin
        Array p = new();
        $display("Array with 'solve before' constraint");
        repeat(12) begin
            p.randomize();
            $display("\tArray of size = %0d, data = %p", p.size, p.data);
        end
    end
end

endprogram
